package com.xauto.pioneer.appuser.wxapi;


import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.util.ToastUtil;

import de.greenrobot.event.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);

        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.e(TAG,"微信支付回调code "+resp.errCode+" ; "+resp.errStr);
//		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
		if (resp.errCode == 0) {
			//支付成功之后给相应界面发送通知
			EventBus.getDefault().post(Constants.PAY_TYPE_WECHAT);
			ToastUtil.showShort(WXPayEntryActivity.this, "微信支付成功");
			finish();
		}else {
			ToastUtil.showShort(WXPayEntryActivity.this, "微信支付失败");
			finish();
		}
	}
}